/*
Copyright (c) 2008 SmashedApples 

Contributors.  See: http://smashedapples.googlecode.com/

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package smashedapples.core
{
	import flash.events.Event;
	
	import mx.containers.Canvas;
	import mx.core.UIComponent;
    
    use namespace smashedns;
    
	public class Component extends Canvas
	{	
		 /**
		 *    internal namespace controller
		 */		
		smashedns var $controller : Controller;
		
		
		
		/**
		 * 
		 *  Constructor 
		 * 
		 */		
		public function Component()
		{
			super();
			this.percentHeight = 100;
			this.percentWidth = 100;
		}
		
		/**
		 *  
		 *  We override the dispatchEvent function so that we can catch all events coming from
		 *  our component
		 * 
		 * @param event 
		 * @return Boolean
		 * 
		 */		
		
		override public function dispatchEvent(event:Event):Boolean
		{	
			if(smashedns::$controller != null)
				smashedns::$controller.execute( new Command(event.type,event,this) )
			return super.dispatchEvent(event);
		}
		
		
	
		
		/**
		 *  Controller  
		 */		
		private var _controller : Class;
		
		public function get controller():Class
		{
			return _controller;
		}
		public function set controller(value:Class):void 
        {	
        	_controller = value;
        	
        	var classRef : Object = new value();
        	if( classRef is Controller)
        	{
        		smashedns::$controller = classRef as Controller;
        	}
        	
        	
        	this.invalidateProperties();	
        }
        
        
        
		
		/**
		 * we override measure(), besides our Component's measure is based upon the child in our component 
		 * 
		 */		
		override protected function measure():void 
		{
			super.measure();

			if(this.getChildren().length == 1)
			{
				var child : UIComponent = this.getChildren()[0] as UIComponent; 
				measuredMinWidth = child.minWidth;
				measuredMinHeight = child.minHeight;
				measuredWidth = child.measuredWidth;
				measuredHeight = child.measuredHeight;
			}
			
		}
		
		
		/**
		 * 
		 * @param unscaledWidth
		 * @param unscaledHeight
		 * 
		 */		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void 
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			if(this.getChildren().length == 1)
			{
				var child : UIComponent = this.getChildren()[0]; 
			 	child.width = unscaledWidth;
			 	child.height = unscaledHeight;	
			}
		
		}
		
		
		
		
		
		
		
	}
}